package com.todo.strategy.online.application.backend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.strategy.common.enums.PlanRelationSceneEnum;
import com.todo.strategy.common.enums.StrategyStatusEnum;
import com.todo.strategy.online.application.backend.service.UserStrategyBackendService;
import com.todo.strategy.online.application.convert.StrategyConvert;
import com.todo.strategy.online.application.entity.bo.*;
import com.todo.strategy.online.application.entity.condition.PlanSetConditionBO;
import com.todo.strategy.online.application.service.PlanContentService;
import com.todo.strategy.online.application.service.PlanRelationService;
import com.todo.strategy.online.application.service.PlanSetService;
import com.todo.strategy.online.application.service.StrategyInfoService;
import com.todo.strategy.online.iface.backend.request.*;
import com.todo.strategy.online.iface.backend.response.QueryUserStrategyByConditionResp;
import com.todo.strategy.online.iface.backend.response.UserStrategyDetailResp;
import com.todo.strategy.online.iface.backend.response.UserStrategyRelationResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (StrategyInfo)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:49
 */
@Slf4j
@Service("userStrategyBackendService")
public class UserStrategyBackendServiceImpl implements UserStrategyBackendService {

    @Resource
    private PlanContentService planContentService;
    @Resource
    private PlanRelationService planRelationService;
    @Resource
    private PlanSetService planSetService;
    @Resource
    private StrategyInfoService strategyInfoService;


    @Override
    public BizResponse<UserStrategyDetailResp> queryUserStrategyDetailById(QueryUserStrategyDetailByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlanSetBO planSetBO = planSetService.queryPlanSetById(req.getPlanId());
        if (planSetBO == null) {
            log.warn("UserStrategyBackendService#queryUserStrategyDetailById 根据计划Id主键查询数据完成，数据为空 默认返回空 req：【{}】", JSONObject.toJSONString(req));
            return BizResponse.success();
        }
        StrategyInfoBO strategyInfoBO = strategyInfoService.queryStrategyInfoPOByPlanId(planSetBO.getUserId(), req.getPlanId());
        return BizResponse.success(StrategyConvert.buildUserStrategyDetailResp(planSetBO, strategyInfoBO));
    }

    @Override
    public BizPageResponse<QueryUserStrategyByConditionResp> queryUserStrategyByCondition(QueryUserStrategyByConditionReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlanSetConditionBO planSetConditionBO = StrategyConvert.convertToPlanSetConditionBO(req);
        PageResult<PlanSetBO> pageResult = planSetService.queryPlanSetByCondition(planSetConditionBO);
        if (pageResult == null) {
            log.error("UserStrategyBackendService#queryUserStrategyByCondition 多条件查询完成，结果为空，默认查询系统异常 req：【{}】", JSONObject.toJSONString(req));
            return BizPageResponse.systemError();
        }
        return BizPageResponse.success(StrategyConvert.convertToQueryUserStrategyByConditionRespPage(pageResult));
    }

    @Override
    public BizResponse<List<UserStrategyRelationResp>> queryUserStrategyRelationList(QueryUserStrategyRelationListReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PlanSetBO planSetBO = planSetService.queryPlanSetById(req.getPlanId());
        if (planSetBO == null) {
            log.warn("UserStrategyBackendService#queryUserStrategyRelationList 根据计划Id主键查询数据完成，数据为空 默认返回空 req：【{}】", JSONObject.toJSONString(req));
            return BizResponse.success();
        }

        List<PlanRelationBO> planRelationBOS = planRelationService.queryPlanRelationByBizId(req.getPlanId(), PlanRelationSceneEnum.USER_STRATEGY);
        if (CollectionUtils.isEmpty(planRelationBOS)) {
            log.warn("UserStrategyBackendService#queryUserStrategyDetailById 根据清单Id查询关联信息完成，数据为空，默认返回空 req：【{}】", JSONObject.toJSONString(req));
            return BizResponse.success();
        }
        List<PlanContentBO> planContentBOS = planContentService.queryPlanContentByPlanId(planSetBO.getUserId(), req.getPlanId());
        return BizResponse.success(StrategyConvert.buildUserStrategyRelationRespList(planRelationBOS, planContentBOS));
    }

    @Override
    public BizResponse<Boolean> prohibitStrategyById(ProhibitStrategyByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        //TODO 目前针对状态调整没有明确的下层方法，暂定
        return null;
    }

    @Override
    public BizResponse<Boolean> detectionUserStrategyById(DetectionUserStrategyReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        StrategyDetailBO strategyDetailBO = strategyInfoService.queryStrategyDetailById(req.getStrategyId());
        if (strategyDetailBO == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        Boolean checkResult;
        if (req.getPass()) {
            checkResult = strategyInfoService.updateStrategyStatus(req.getStrategyId(), StrategyStatusEnum.AUTO_EXAMINE_FAILED, StrategyStatusEnum.PUBLISHED);
        } else {
            checkResult = strategyInfoService.updateStrategyStatus(req.getStrategyId(), StrategyStatusEnum.AUTO_EXAMINE_FAILED, StrategyStatusEnum.EXAMINE_FAILED);
        }
        log.info("UserStrategyBackendService#detectionUserStrategyById 检测用户攻略完成，检测结果为：【{}】 req：【{}】", checkResult, JSONObject.toJSONString(req));
        return BizResponse.success(checkResult);
    }
}
